package com.dtdream.sellergoods.service.impl;

import java.util.List;
import java.util.Map;

import com.dtdream.pojo.TbBrandExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.dtdream.mapper.TbBrandMapper;
import com.dtdream.pojo.TbBrand;
import com.dtdream.sellergoods.service.BrandService;

@Service
public class BrandServiceImpl implements BrandService {

	
	@Autowired
	private TbBrandMapper brandMapper;
	//查询品牌列表
	@Override
	public List<TbBrand> findAll() {
		return brandMapper.selectByExample(null);
	}
	//返回品牌分页列表
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);
		return new PageResult(page.getTotal(),page.getResult());
	}
	//添加
	@Override
	public void add(TbBrand tbBrand) {
		brandMapper.insert(tbBrand);
	}
	//更新
	@Override
	public void updata(TbBrand tbBrand) {
		brandMapper.updateByPrimaryKey(tbBrand);
	}
	//根据ID获取实体
	@Override
	public TbBrand findOne(Long id) {
		TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
		return tbBrand;
	}
	//批量删除
	@Override
	public void delect(Long[] ids) {
		for (Long id:ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}
	/**
	 * 品牌条件查询(带分页)
	 * 当前页
	 * 每页记录数
	 */
	@Override
	public PageResult findPage(TbBrand tbBrand, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);
		TbBrandExample example = new TbBrandExample();
		TbBrandExample.Criteria criteria = example.createCriteria();
		if(tbBrand.getName() != null && tbBrand.getName().length()>0){
			criteria.andNameLike("%"+tbBrand.getName()+"%");
		}
		if(tbBrand.getFirstChar()!=null && tbBrand.getFirstChar().length()>0){
			criteria.andFirstCharEqualTo(tbBrand.getFirstChar());
		}
		Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(example);
		return new PageResult(page.getTotal(),page.getResult());
	}

	/**
	 * 品牌下拉框数据
	 * @return
	 */
	@Override
	public List<Map> selectOptionList() {
		return brandMapper.selectOptionList();
	}
}
